package com.qf.web.controller;

import com.alibaba.fastjson.JSON;
import com.qf.domain.GoodsType;
import com.qf.domain.User;
import com.qf.service.GoodsTypeService;
import com.qf.service.impl.GoodTypeServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * projectName:day15_myxiaomi
 * author:lcy
 * time:2023/11/27 14:13
 * description:
 */
@WebServlet("/goodstypeservlet")
public class GoodsTypeServlet extends BaseServlet {

    public String goodstypelist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        ServletContext application = this.getServletContext();
        String goodsType = (String) application.getAttribute("goodsType");
        if (goodsType != null) {
            response.getWriter().write(goodsType);
            System.out.println("从application中获取数据");
            return null;
        }

        GoodsTypeService goodsTypeService = new GoodTypeServiceImpl();

        List<GoodsType> goodsTypes = goodsTypeService.findByLevel(1);
        System.out.println("从数据库中获取数据");

        String json = JSON.toJSONString(goodsTypes);

        response.getWriter().write(json);

        application.setAttribute("goodsType", json);

        return null;
    }

    //goodstypeservlet?method=addGoodsType goodstypeservlet?method=addGoodsType
    public String addGoodsType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String level = request.getParameter("level");
        String parent = request.getParameter("parent");
        //if(StringUtils.isEmpty(name)){
        //    request.setAttribute("msg", "分类姓名不能为空");
        //    return "message.jsp";
        //}
        //if(StringUtils.isEmpty(level)){
        //    request.setAttribute("msg", "分类等级不能为空");
        //    return "message.jsp";
        //}
        //if(StringUtils.isEmpty(parent)){
        //    request.setAttribute("msg", "分类parent不能为空");
        //    return "message.jsp";
        //}
        GoodsTypeService goodsTypeService = new GoodTypeServiceImpl();
        GoodsType goodsType = new GoodsType(0, name, Integer.parseInt(level), Integer.parseInt(parent));
        try {

            goodsTypeService.AddType(goodsType);
            return "redirect:/goodstypeservlet?method=getGoodsType";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "添加商品类别失败：" + e.getMessage());
            return "admin/message.jsp";
        }
    }

    //goodstypeservlet?method=getGoodsType&level="+level+"&name="+name
    public String getGoodsType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进来了");
        String level = request.getParameter("level");
        String name = request.getParameter("name");

        List<Object> params = new ArrayList<>();
        StringBuilder where = new StringBuilder("where 1=1");
        if (!StringUtils.isEmpty(name)) {
            where.append(" and name like? ");
            params.add("%" + name + "%");
        }
        if (!StringUtils.isEmpty(level)) {
            where.append(" and level=? ");
            params.add(level);
        }
        GoodsTypeService goodsTypeService = new GoodTypeServiceImpl();
        try {
            List<GoodsType> goodsTypeList = goodsTypeService.findAll(where.toString(), params);
            request.getSession().setAttribute("goodsTypeList", goodsTypeList);
            return "admin/showGoodsType.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询失败：" + e.getMessage());
            return "admin/message.jsp";
        }
    }

    //goodstypeservlet?method=deleteGoodsType&id=${gtype.id}
    public String deleteGoodsType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        try {
            GoodsTypeService goodsTypeService = new GoodTypeServiceImpl();
            goodsTypeService.deleteTid(id);
            return "admin/addGoodsType.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "删除失败");
            return "admin/message.jsp";
        }
    }
//goodstypeservlet?method=findGoodsType&id=${gtype.id}
    public String findGoodsType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        System.out.println("id = " + id);
        try {
            GoodsTypeService goodsTypeService=new GoodTypeServiceImpl();
           GoodsType goodsType= goodsTypeService.find(id);
           request.setAttribute("goodsType",goodsType);
           return "/admin/Update.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "失败"+ e.getMessage());
            return "admin/message.jsp";
        }
    }
    //goodstypeservlet?method=modifyGoodsType&id=${gtype.id}
    public String modifyGoodsType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String level = request.getParameter("level");
        String parent = request.getParameter("author");
        System.out.println(id);
        System.out.println(name);
        System.out.println(level);
        System.out.println(parent);

        try {
            GoodsTypeService goodsTypeService=new GoodTypeServiceImpl();
            goodsTypeService.modify(Integer.parseInt(id),name,Integer.parseInt(level),Integer.parseInt(parent));
            return "redirect:/goodstypeservlet?method=getGoodsType";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "修改失败"+ e.getMessage());
            return "admin/message.jsp";
        }
    }
}